package LeedCode_BinaryTree;

/**
 * Created with IntelliJ IDEA.
 * Description:相同的树
 * User: 阿宾
 * Date: 2024-11-20
 * Time: 02
 */
public class Demo1 {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public TreeNode createTree1(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('K');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        //判断空树
        if(p == null && q == null){
            return true;
        }
        //判断一个为空，一个不为空
        if(p == null || q == null){
            return false;
        }
        //都不空,判断值
        if(p.val != q.val){
            return false;
        }
        boolean ret1 = isSameTree(p.left,q.left);
        boolean ret2 = isSameTree(p.right,q.right);
        return ret1 && ret2;

    }
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        TreeNode root1 = demo1.createTree();
        TreeNode root2 = demo1.createTree1();
        boolean ret = isSameTree(root1, root2);
        System.out.println(ret);


    }
}
